﻿package mortal.game.view.loginNotice
{
    import com.gengine.debug.*;
    import com.gengine.utils.*;
    import extend.language.*;
    import flash.events.*;
    import flash.net.*;
    import flash.utils.*;
    import mortal.common.global.*;

    public class LoginNoticeDownload extends Object
    {
        private var _roleTips:String;
        private var _addList:Array;
        private var _updateList:Array;
        public var versionKey:String = "localVersionKey";
        public var versionChangeKey:String = "localVersionChangeKey";
        private var _loadDataCall:Function;
        private var _loader:URLLoader;
        private static var _instance:LoginNoticeDownload;

        public function LoginNoticeDownload()
        {
            this._addList = [];
            this._updateList = [];
            return;
        }// end function

        public function loadDataFromPhp(param1:String, param2:Function) : void
        {
            this._loadDataCall = param2;
            this._loader = new URLLoader();
            Log.debug(param1 + "LoginNotice_" + ParamsConst.Version + ".xml" + "?" + getTimer());
            var _loc_3:* = new URLRequest(param1 + "LoginNotice_" + ParamsConst.Version + ".xml" + "?" + getTimer());
            this._loader.addEventListener(Event.COMPLETE, this.onVersionDataLoaded);
            this._loader.addEventListener(IOErrorEvent.IO_ERROR, this.onVersionDataIOError);
            this._loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, this.onSecurityError);
            this._loader.load(_loc_3);
            return;
        }// end function

        private function onVersionDataLoaded(event:Event) : void
        {
            this.writeXml(new XML(this._loader.data));
            try
            {
                this._loader.close();
            }
            catch (e)
            {
            }
            this._loader.removeEventListener(Event.COMPLETE, this.onVersionDataLoaded);
            this._loader.removeEventListener(IOErrorEvent.IO_ERROR, this.onVersionDataIOError);
            this._loader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, this.onSecurityError);
            this._loader = null;
            if (this._loadDataCall != null)
            {
                this._loadDataCall();
            }
            return;
        }// end function

        private function onVersionDataIOError(event:IOErrorEvent) : void
        {
            try
            {
                this._loader.close();
            }
            catch (e)
            {
            }
            this._loader.removeEventListener(Event.COMPLETE, this.onVersionDataLoaded);
            this._loader.removeEventListener(IOErrorEvent.IO_ERROR, this.onVersionDataIOError);
            this._loader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, this.onSecurityError);
            this._loader = null;
            Log.debug("版本更新文件下载失败" + event.text);
            return;
        }// end function

        private function onSecurityError(event:SecurityErrorEvent) : void
        {
            try
            {
                this._loader.close();
            }
            catch (e)
            {
            }
            this._loader.removeEventListener(Event.COMPLETE, this.onVersionDataLoaded);
            this._loader.removeEventListener(IOErrorEvent.IO_ERROR, this.onVersionDataIOError);
            this._loader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, this.onSecurityError);
            this._loader = null;
            Log.debug("版本更新文件下载失败", event.text);
            return;
        }// end function

        public function writeXml(param1:XML) : void
        {
            this._addList.splice(0);
            this._updateList.splice(0);
            if (param1.hasOwnProperty("rolltip"))
            {
                this._roleTips = StringHelper.getString(param1.rolltip.@msg);
            }
            if (param1.hasOwnProperty("add"))
            {
                this.parseXml(param1.add.notices.notice, Language.getString(11801), this._addList, 0);
            }
            if (param1.hasOwnProperty("update"))
            {
                this.parseXml(param1.update.notices.notice, Language.getString(11802), this._updateList, 1);
            }
            return;
        }// end function

        private function parseXml(param1:XMLList, param2:String, param3:Array, param4:int) : void
        {
            var _loc_5:XML = null;
            var _loc_6:LoginNoticeInfo = null;
            var _loc_8:XMLList = null;
            var _loc_9:XML = null;
            var _loc_7:int = 0;
            for each (_loc_5 in param1)
            {
                
                _loc_6 = new LoginNoticeInfo();
                _loc_6.type = param4;
                _loc_6.index = _loc_7;
                _loc_6.title = param2;
                _loc_6.notice = _loc_5.@des;
                if (_loc_5.hasOwnProperty("item"))
                {
                    _loc_8 = _loc_5.item;
                    for each (_loc_9 in _loc_8)
                    {
                        
                        if (int(_loc_9.@id) != 0)
                        {
                            _loc_6.itemIds.push(int(_loc_9.@id));
                        }
                    }
                }
                _loc_6.updateData();
                param3.push(_loc_6);
                _loc_7++;
            }
            return;
        }// end function

        public function get noticeMsg() : String
        {
            return this._roleTips;
        }// end function

        public function get addList() : Array
        {
            return this._addList;
        }// end function

        public function get updateList() : Array
        {
            return this._updateList;
        }// end function

        public static function get instance() : LoginNoticeDownload
        {
            if (!_instance)
            {
                _instance = new LoginNoticeDownload;
            }
            return _instance;
        }// end function

    }
}
